using System;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;

namespace FireStarterModeller.UI
{
    public class DMClassSelectorControl : UserControlWin, IFormControl, IBOParentChildrenSelector
    {
        private DMAssembly _assembly;
        public IGridControl GridControl { get; private set; }

        public event EventHandler<BOEventArgs<DMClass>> ClassSelected;

        public DMClassSelectorControl(IControlFactory controlFactory)
        {
            BusinessObjectCollection<DMClass> col = new BusinessObjectCollection<DMClass>();
            GridControl = controlFactory.CreateEditableGridControl();
            GridControl.Width = 250;
            GridControl.BusinessObjectCollection = col;
            if (GridControl.Buttons["Add"] != null) GridControl.Buttons["Add"].Visible = false;
            if (GridControl.Buttons["Edit"] != null) GridControl.Buttons["Edit"].Visible = false;
            if (GridControl.Buttons["Delete"] != null) GridControl.Buttons["Delete"].Visible = false;

            GridControl.Buttons.Visible = false;

            GridControl.FilterControl.Visible = true;
            GridControl.FilterControl.AddStringFilterTextBox("", "ClassNameBO");

            BorderLayoutManager mainLayoutManager = controlFactory.CreateBorderLayoutManager(this);
            mainLayoutManager.AddControl(GridControl, BorderLayoutManager.Position.Centre);
            GridControl.BusinessObjectSelected +=
                    (sender, e) => FireClassSelected((DMClass)GridControl.SelectedBusinessObject);

            this.Enabled = false;
        }

        public DMClass Class
        {
            get { return (DMClass)GridControl.SelectedBusinessObject; }
            set
            {
                try
                {
                    GridControl.SelectedBusinessObject = value;
                }
                catch (Exception ex)
                {
                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                }
            }
        }

        public DMAssembly Assembly
        {
            get { return _assembly; }
            set
            {
                try
                {
                    _assembly = value;
                    if (_assembly != null)
                    {
                        this.Enabled = true;
                        GridControl.BusinessObjectCollection = _assembly.Classes;
                    }
                    else
                    {
                        this.Enabled = false;
                        GridControl.BusinessObjectCollection = new BusinessObjectCollection<DMClass>();
                    }
                }
                catch (Exception ex)
                {
                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                }
            }
        }

        public BusinessObjectCollection<DMClass> Classes
        {
            get { return (BusinessObjectCollection<DMClass>)GridControl.BusinessObjectCollection; }
        }

        private void FireClassSelected(DMClass dmClass)
        {
            try
            {
                if (this.ClassSelected != null)
                {
                    this.ClassSelected(this, new BOEventArgs<DMClass>(dmClass));
                }
                if (this.BusinessObjectSelected != null) 
                {
                    this.BusinessObjectSelected(this, new BOEventArgs(dmClass));
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        public void SetForm(IFormHabanero form)
        {
        }

        IBusinessObject IBusinessObjectControl.BusinessObject
        {
            get
            {
                return Assembly;
            }
            set { Assembly = (DMAssembly)value; }
        }

        #region IBOColSelector

        public event EventHandler<BOEventArgs> BusinessObjectSelected;
        public void Clear()
        {
            this.GridControl.Clear();
        }

        public IBusinessObject GetBusinessObjectAtRow(int row)
        {
            return this.GridControl.GetBusinessObjectAtRow(row);
        }

        public IBusinessObjectCollection BusinessObjectCollection
        {
            get { return this.GridControl.BusinessObjectCollection; }
            set { this.GridControl.BusinessObjectCollection = value; }
        }

        public IBusinessObject SelectedBusinessObject
        {
            get { return this.GridControl.SelectedBusinessObject; }
            set { this.GridControl.SelectedBusinessObject = value; }
        }

        public int NoOfItems
        {
            get { return this.GridControl.NoOfItems; }
        }

        /// <summary>
        ///             Gets and sets whether this selector autoselects the first item or not when a new collection is set.
        /// </summary>
        public bool AutoSelectFirstItem
        {
            get { return GridControl.AutoSelectFirstItem; }
            set { GridControl.AutoSelectFirstItem = value; }
        }

        #endregion

    }
}
